package com.example.lib_apply.detail

import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.LogUtils
import com.example.lib_apply.ApplyEntity
import com.example.lib_common.mvi.BaseViewModel
import com.example.lib_common.mvi.ResponseEntity
import com.google.gson.Gson
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch

class DetailViewModel : BaseViewModel<DetailRepository,DetailState>() {
    override fun createRepo(): DetailRepository {
        return DetailRepository()
    }

    override fun createState(): DetailState {
        var list :List<ParkingEntity> =ArrayList()
        var list1 :List<VisitorEntity> =ArrayList()
        return DetailState(ResponseEntity(0,"", list),
                            ResponseEntity(0,"", list1))
    }

    fun parkingStep(map: HashMap<String,Any>){
        mutableContainer.updateRequestState { copy(
            request=true
        ) }
        viewModelScope.launch(Dispatchers.IO) {
            repo.requestParkingEntity(map).flowOn(Dispatchers.IO)
                .collect{
                mutableContainer.updateRequestState {
                    copy(request=false,complete=true)
                }
                mutableContainer.updateUiState { copy(
                    entity=it
                ) }
            }
        }
    }

    fun visitorStep(map: HashMap<String,Any>){
        mutableContainer.updateRequestState { copy(
            request=true
        ) }
        viewModelScope.launch(Dispatchers.IO) {
            repo.requestVisitorEntity(map).flowOn(Dispatchers.IO)
                .collect{
                    mutableContainer.updateRequestState {
                        copy(request=false,complete=true)
                    }
                    mutableContainer.updateUiState { copy(
                        entity1=it
                    ) }
                }
        }
    }
}